//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    NotificationRulesPropertyPage.cs
//    
// Abstract:
//
//    Implements the automatic notifications property page UI.
//
//--

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Microsoft.OfficeCommunicationsServer.Applications.Common;

namespace Microsoft.EnhancedReminders.OutlookAddIn
{
    /// <summary>
    /// Automatic notifications property page logic.  Note that this cannot be
    /// instantiated directly from the Outlook property pages and needs to be
    /// embedded in a WinForm control due to COM visibility issues with the WPF
    /// parent classes.
    /// </summary>
    public partial class NotificationRulesPropertyPage : UserControl
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public NotificationRulesPropertyPage()
        {
            InitializeComponent();

            Loaded += NotificationRulesPropertyPage_Loaded;
        }

        /// <summary>
        /// Called when the control is loaded by WPF.
        /// </summary>
        private void NotificationRulesPropertyPage_Loaded(object sender, RoutedEventArgs e)
        {
            List<NotificationChannel> channels = null;
            OutlookApplication application = new OutlookApplication(Globals.ThisAddIn.Application);

            try
            {
                channels = ReminderOutlookUtils.LoadChannels(application, /*restoreDefaultsIfNecessary*/true);
            }
            catch (FileLoadException exn)
            {
                // This can be caused by issues (e.g. signing issues) to do with loading CommunicatorAPI.dll.
                Logger.WriteTraceError(exn, "Error loading CommunicatorAPI.dll.  If you are building this application, make sure that CommunicatorAPI.dll does not have strong name verification enabled");
                return;
            }
      
            channelControl.Initialize(channels);
        }

        /// <summary>
        /// Save the channels to Outlook storage.
        /// </summary>
        public void SaveChannels()
        {
            try
            {
                ReminderOutlookUtils.SaveChannels(channelControl.Channels.Channels, 
                                                  new OutlookApplication(Globals.ThisAddIn.Application));
            }
            catch (InvalidOperationException exn)
            {
                // Warn the user if an error occurs while saving the channels.
                MessageBox.Show(String.Format(CultureInfo.CurrentUICulture, Properties.Resources.FormatChannelsSaveWarning, exn.ToString()),
                                Properties.Resources.ChannelControlWarningTitle,
                                MessageBoxButton.OK,
                                MessageBoxImage.Warning);
            }
            catch (FileLoadException exn)
            {
                // This can be caused by issues (e.g. signing issues) to do with loading CommunicatorAPI.dll.
                Logger.WriteTraceError(exn, "Error loading CommunicatorAPI.dll.  If you are building this application, make sure that CommunicatorAPI.dll does not have strong name verification enabled");
            }
        }

        /// <summary>
        /// Invokes the notification rules dialog when the "Rules" button is clicked.
        /// </summary>
        private void OnRulesButtonClicked(object sender, RoutedEventArgs e)
        {
            NotificationRulesDialog rulesDlg = new NotificationRulesDialog();
            rulesDlg.ShowDialog();
        }
    }
}
